package com.itjason.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import cn.binarywang.wx.miniapp.util.WxMaConfigHolder;
import com.itjason.mapper.WxUserMapper;
import com.itjason.dto.UserLoginDataDTO;
import com.itjason.exceptionhandler.EnumException;
import com.itjason.exceptionhandler.ResponseException;
import com.itjason.po.UserInfoPO;
import com.itjason.service.WxUserService;
import com.itjason.vo.UserInfoVO;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Author:JasonLong
 * Date:2023/5/9
 */
@Service
public class WxUserServiceImpl implements WxUserService {

    @Autowired
    private WxMaService wxMaService;

    @Autowired
    private WxUserMapper wxUserMapper;

    /**
     * @param userLoginDataDTO
     * @return loginState
     * 此业务设计为:
     * 1、用户初次登录，将在数据库保存用户的头像，昵称，以及openid
     * 2、用户非初次登录，将获取到的openid在数据库中匹配，若存在，则表示该用户已注册过
     * 将无需再去获取用户相关信息
     */
    @Override
    public ResponseException saveLoginInfo(UserLoginDataDTO userLoginDataDTO) {
        try {
            WxMaJscode2SessionResult session = wxMaService.getUserService().getSessionInfo(userLoginDataDTO.getCode());
            //拿到OpenId
            String openid = session.getOpenid();
            //拿到sessionKey
            String sessionKey = session.getSessionKey();
            //查询数据库
            String s = wxUserMapper.selectByOpenId(openid);
            boolean empty = s.isEmpty();
            //满足该条件即该用户不存在
            if (empty) {
                //获取用户相关信息并存入数据库
                WxMaUserInfo userInfo = wxMaService.getUserService().getUserInfo(sessionKey, userLoginDataDTO.getEncryptedData(), userLoginDataDTO.getIv());
                System.out.println(userInfo);
                String avatarUrl = userInfo.getAvatarUrl();
                String nickName = userInfo.getNickName();
                //持久化对象封装数据
                UserInfoPO userInfoPO = new UserInfoPO();
                userInfoPO.setUserNickname(nickName);
                userInfoPO.setUserAvatarurl(avatarUrl);
                userInfoPO.setUserOpenid(openid);
                //数据持久化到数据库
                int insert = wxUserMapper.insert(userInfoPO);
                System.out.println(insert > 0 ? "insert_yes" : "insert_no");
                return ResponseException.success();
            } else {
                System.out.println("该用户已注册，直接登录！");
                return ResponseException.success();
            }
        } catch (WxErrorException e) {
            e.printStackTrace();
            return ResponseException.error(EnumException.INTERNAL_SERVER_ERROR.getResultCode(), EnumException.INTERNAL_SERVER_ERROR.getResultMsg());
        } finally {
            WxMaConfigHolder.remove();//清理ThreadLocal
        }
    }

    @Override
    public String register() {
        return null;
    }

    @Override
    public UserInfoVO getUserInfo(String userToken) {

        return null;
    }
}
